<template>
  <div class="goods-item">
    <van-image class="goods-img" @click="toInfo" width="100%" height="140" :src="goods.goods_img" />

    <div class="top-wrap">
      <span class="goods-name">{{goods.goods_name}}</span>
    </div>

    <div class="center-wrap">
      <div class="left">
        <span class="get-time">提货时间: {{goods.get_time}}</span>
      </div>
      <div class="right">
        <div class="fenshu">
          <span>已售{{goods.be_sold}}份</span>
          <span>剩余{{goods.unsold}}份</span>
        </div>
      </div>
    </div>

    <div class="bottom-wrap">
      <div class="price">
        <span class="price-current">￥{{goods.current_price}}</span>
        <span class="price-original">￥{{goods.origin_price}}</span>
      </div>
      <van-button @click="addToGoodsCar(goods.goodsId)" type="danger" class="goods-car" round>加入购物车</van-button>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  name: 'GoodsItem',
  props: {
    goods: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      isGoodsBuyShow: false // 购买弹出层显示状态
    }
  },
  created () {

  },
  mounted () {

  },
  watch: {},
  computed: {
    ...mapState(['goodsCarList'])
  },
  methods: {
    ...mapMutations(['setGoodsInfo', 'addGoodsCar']),
    // 图片点击跳转到商品详情页面
    toInfo () {
      setItem('goodsInfoData', this.goods)
      this.setGoodsInfo(this.goods)
      this.$router.push('/goods/buy')
    },
    // 加入购物车
    addToGoodsCar(goodsId) {
      const temp = this.goodsCarList.findIndex(item => {
        return item.goodsId === goodsId
      })
      if (temp === -1) {
        // 购物车没有该商品
        this.addGoodsCar(this.goods)
        setItem('goodsCarListData', this.goodsCarList)
      } else {
        // 购物车有该商品
        this.$toast.fail('该商品已存在购物车，加入失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.goodsBuy {
  z-index: 2;
}
.goods-item {
  border-radius: 20px;
  position: relative;
  //background-color: pink;
  box-shadow: grey 0px 0px 10px;
}
.goods-name {
  font-size: 30px;
  font-weight: 600;
  padding-left: 5px;
}
.center-wrap {
  font-size: 10px;
  color: red;
  height: 60px;
  .left {
    position: absolute;
    left: 10px;
  }
  .right {
    position: absolute;
    right: 10px;
  }
  .fenshu {
    display: flex;
    flex-direction: column;
  }
}
.bottom-wrap {
  margin-top: 10px;
  height: 60px;
  display: flex;
  justify-content: space-between;
  .goods-car {
    width: 190px;
    height: 45px;
    margin-right: 10px;
    font-size: 20px;
  }
  .price-current {
    color: red;
    font-size: 35px;
    i {
      color: #909090;
      font-size: 22px;
    }
  }
  .price-original {
    text-decoration: line-through;
    font-size: 25px;
    color: #909090;
  }
}
</style>
